package com.settlement.system.service.impl;

import com.settlement.system.common.util.common.RedisUtil;
import com.settlement.system.mapper.TblWpManualItemDiffMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblWpManualItemDiff;
import com.settlement.system.service.ITblWpManualItemDiffService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.settlement.system.mapper.BaseMapper;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 唯品会账手工差异账单 服务实现类
 * @author Ximi
 * @since 2020-04-17
 */
@Service
public class TblWpManualItemDiffServiceImpl extends BaseServiceImpl implements ITblWpManualItemDiffService {

    @Resource
    private TblWpManualItemDiffMapper tblWpManualItemDiffMapper;

    @Resource
    private RedisTemplate redisTemplate;

    private String uniqueItemDiffIdKey = "unique_item_diff_id";

    @Override
    public BaseMapper init() {
        return tblWpManualItemDiffMapper;
    }

    /**
     * 获取合计
     * @param params
     * @return
     * @throws ServiceException
     */
    @Override
    public TblWpManualItemDiff selectFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblWpManualItemDiffMapper.selectFoot(params);
        } catch (Exception ex){
            throw new ServiceException("唯品会账手工差异账单合计异常",ex);
        }
    }

    /**
     * 更新并保存
     * @param list
     * @throws ServiceException
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void updateOrSave(List<TblWpManualItemDiff> list) throws ServiceException {
        try {
            Map<String,Object> params = new HashMap<>(16);
            for(TblWpManualItemDiff info:list){
                if(info.getGlobalId() == null){
                    info.setGlobalId(RedisUtil.incr(uniqueItemDiffIdKey,1)+"");
                    tblWpManualItemDiffMapper.insert(info);
                }else {
                    tblWpManualItemDiffMapper.updateByGlobalId(info);
                }
            }
        } catch (Exception ex){
            throw new ServiceException("更新并保存异常",ex);
        }
    }

    /**
     * 查询 列表中 非【衣酷】 的数据有多少条
     * @param idList
     * @return
     * @throws ServiceException
     */
    @Override
    public int selectCountNotYk(List <String> idList, String toBeDeletedScheduleId) throws ServiceException {
        try {
            return tblWpManualItemDiffMapper.selectCountNotYk(idList,toBeDeletedScheduleId);
        } catch (Exception e) {
            throw new ServiceException(e);
        }
    }

}

